﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using VisualAgile.IBLL;
using VisualAgile.Model;
using System.Collections;

namespace VisualAgile.Demo
{
    public class FakeTaskManager : ITaskManager   
    {
        private List<Task> taskList;

        public FakeTaskManager(List<Task> tasks)
        {
            taskList = tasks;
        }

        /// <summary>
        /// Create a task (back to contain the default value)
        /// </summary>
        /// <returns></returns>
        public Task NewTask()
        {
            var task = new Task();
            
            task.CreateTime = DateTime.Now;            

            return task;
        }

        /// <summary>
        /// Save task
        /// </summary>
        /// <param name="task">Task entity</param>
        /// <returns></returns>
        public Task Save(Task task)
        {
            Validate(task);
            if (Get(task.TaskID) != null)
                throw new Exception("Existing task");
            task.TaskID = Guid.NewGuid();
            taskList.Add(task);

            return Get(task.TaskID);
        }

        /// <summary>
        /// Access to tasks
        /// </summary>
        /// <param name="taskId">Task ID</param>
        /// <returns></returns>
        public Task Get(Guid taskId)
        {
            return taskList.SingleOrDefault(u => u.TaskID == taskId);
        }

        /// <summary>
        /// Access to tasks
        /// </summary>
        /// <param name="taskId">Task ID</param>
        /// <returns></returns>
        public IList GetByProject(Guid projectId)
        {
            return null;
        }

        /// <summary>
        /// Access to tasks
        /// </summary>
        /// <param name="taskId">Task ID</param>
        /// <returns></returns>
        public IList GetByStatus(string statusId)
        {
            return taskList.FindAll(f => f.Status.StatusID == new Guid(statusId));
        }

        /// <summary>
        /// Access to tasks
        /// </summary>
        /// <param name="taskId">Task ID</param>
        /// <returns></returns>
        public IList GetByIteration(string iterationId)
        {
            return taskList.FindAll(f => f.IterationID.ToLower() == iterationId.ToLower());
        }

        /// <summary>
        /// Access to tasks
        /// </summary>
        /// <param name="taskId">Task ID</param>
        /// <returns></returns>
        public IList GetNonCatagorizedTasks()
        {
            return taskList.FindAll(f => f.Story == null);
        }


        /// <summary>
        /// Access to tasks
        /// </summary>
        /// <param name="taskId">Task ID</param>
        /// <returns></returns>
        public IList GetByStory(string storyId)
        {
            return taskList.FindAll(t => t.Story != null && t.Story.StoryID == new Guid(storyId));
        }      


        /// <summary>
        /// Access to tasks
        /// </summary>
        /// <param name="taskId">Task ID</param>
        /// <returns></returns>
        public IList GetByStories(string storyList)
        {
            List<string> names = storyList.Split(',').ToList();            

            return (from task in taskList 
                    where names.Contains(task.Story.StoryID.ToString()) 
                    select task
                    ).ToList();            
        }
        

        /// <summary>
        /// Delete Task
        /// </summary>
        /// <param name="taskId">Task ID</param>
        /// <returns></returns>
        public void Delete(Guid taskId)
        {
            var task = Get(taskId);
            if (task == null) throw new Exception("NotFound");
            taskList.Remove(task);
        }

        /// <summary>
        /// Update Task
        /// </summary>
        /// <param name="task">Task Entity</param>
        /// <returns></returns>
        public Task Update(Task task)
        {
            Task oldTask = Get(task.TaskID);
            if (oldTask != null)
                taskList.Remove(oldTask);
            else
                task.TaskID = Guid.NewGuid();

            taskList.Add(task);
            return Get(task.TaskID);
        }

        /// <summary>
        /// Access to tasks
        /// </summary>
        /// <returns></returns>
        public IList GetAll()
        {
            return taskList;
        }

        private void Validate(Task task)
        {
            //TODO throw exception on error.
        }        
    }
}
